<?php
include_once "../header1.php";
?>
<script type="text/javascript">
function getHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function showHint(str)
{
var xmlhttp;    
if (str=="")
  {
  document.getElementById("List").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("List").innerHTML=xmlhttp.responseText;
    }
  }
   var sch_id=document.forms["form1"]["sch_id"].value;
  str=str+","+sch_id;
  
xmlhttp.open("GET","upp_tea_names.php?q="+str,true);
xmlhttp.send();
}
function fill(val)
{
document.form1.TeaName.value=val;
document.getElementById("List").innerHTML="";
var xmlhttp;    
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("TID").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","tra_ids.php?q="+val,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
include_once ("../Login/databaseconnection.php");

if(!loggedin())
{
	require_once ("../index.php");
}
   
   else
   {
if(isset($_POST['SaveChanges']))
   {
   $Teacher_NIC=$_SESSION['Teacher_NIC'];
 if($_SESSION['Access_Type']=="Teacher in Charge")
{
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  $get_teaSch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$Teacher_NIC'");
  if($rowsesch=mysql_fetch_array($get_teaSch))
{
$schid=$rowsesch['School_Id'];
if($schid==$Own_School_Id)
{
   require_once 'UPP.php';
}
else
{
 $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
}
}
else
   {
   $_SESSION['errornotallo']="This person's record is not registered into Teacher's record!";
   }
}
}
 else if($_SESSION['Access_Type']=="Administrator")
{
require_once 'UPP.php';
}
else
{
 $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
}
}
$Training_Id="";
$Teacher_NIC="";
if(isset($_POST['Edit_This']))
	   {	
	   	   	    $Training_Name=$_POST['Training_Name'];
	   	  $resultnet=mysql_query("SELECT * FROM training WHERE Training_Name='$Training_Name'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Training_Id=$rownet['Training_Id'];
}
	  
	  $TeaName=$_POST['TeaName'];
$name=explode(" ",$TeaName);
$fn=$name[0];
$ln=$name[1];
$results= mysql_query("SELECT * FROM teacher Where First_Name='$fn' AND Last_Name='$ln'");
if($row = mysql_fetch_array($results))
{
 $Teacher_NIC=$row['T_NIC_Number'];
}
	 
	  $_SESSION['Teacher_NIC']=$Teacher_NIC;
   $_SESSION['Training_Id']=$Training_Id;
	if($_SESSION['Access_Type']=="Teacher in Charge")
{
 if( $Training_Name=="Select One"||$TeaName=="" )
	   {		    
	$_SESSION['errorin1']="Please select the Teacher's name number and Training name!";
}
 else
	   {
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  $get_teaSch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$Teacher_NIC'");
  if(mysql_num_rows($get_teaSch)==0)
{
$_SESSION['errornotallo']="This person's record is not registered into Teacher's record!";
}
    if(mysql_num_rows($get_teaSch)>0)
{
  if($rowsesch=mysql_fetch_array($get_teaSch))
{
$schid=$rowsesch['School_Id'];
if($schid==$Own_School_Id)
{
	   $result=mysql_query("SELECT * FROM training_details WHERE Training_Id='$Training_Id' AND Teacher_NIC='$Teacher_NIC' ");
	   if(mysql_num_rows($result)==0)
   {
$_SESSION['errornorow']="Very sorry there is no result,Please check your inputs and try again!";
   }
   
}
else
{
 $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
}
}
}
}
 else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
}
}   
else if($_SESSION['Access_Type']=="Administrator")
{
 if( $Training_Name=="Select One"||$TeaName=="" )
	   {
		    
	$_SESSION['errorin1']="Please select the Teacher's name number and Training name!";
}
	   else
	   {
	   $result=mysql_query("SELECT * FROM training_details WHERE Training_Id='$Training_Id' AND Teacher_NIC='$Teacher_NIC' ");
	   if(mysql_num_rows($result)==0)
   {
$_SESSION['errornorow']="Very sorry there is no result,Please check your inputs and try again!";
   }
   }
} 
else
{
 $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
} 
}
   ?>
    <?php

include_once "../header2.php";


?>
<?php
include_once "topmenu.php";
?>
<div id="content">
	<div id="main">
	  <div id="bodyfield">
	  <div id="lefttablereg">
<form action="#" method="POST" name="form1"><br/>
  <table width="562" border="0" class="tablefontv">
    <tr>
      <td colspan="4"  scope="row" class="err"><?php
		if(isset($_POST['Edit_This'])||isset($_POST['SaveChanges']))
{
 if(isset($_SESSION['error']))
	{
	echo'<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['error'];
		unset($_SESSION['error']);
	}
	if(isset($_SESSION['errorin']))
	{echo'<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errorin'];
		unset($_SESSION['errorin']);
	}
	if(isset($_SESSION['errorin1']))
	{echo'<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errorin1'];
		unset($_SESSION['errorin1']);
	}
	if(isset($_SESSION['errornorow']))
	{echo'<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errornorow'];
		unset($_SESSION['errornorow']);
	}
		if(isset($_SESSION['errornotallo']))
	{echo'<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errornotallo'];
		unset($_SESSION['errornotallo']);
	}
}
		?>
		</td>
      </tr>
    <tr>
      <th width="2"  scope="row">&nbsp;</th>
      <th width="176"  scope="row" bgcolor="#6AB5FF">Teacher's Name:</th>
      <td width="92" bgcolor="#999999">
       <?php
 $hin=$_SESSION['NIC_Number'].",".$_SESSION['Access_Type'];
		   echo "<input type='hidden' size='1' id='sch_id' name='sch_id' value='$hin'/>";
	  if(!isset($TeaName))
	{
	  $tnam=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$Teacher_NICc'");
	  if($rot=mysql_fetch_array($tnam))
	  {
	  $TeaName=$rot['First_Name']." ".$rot['Last_Name'];
	  }
	  }	
	  echo "<input type='text' id='TeaName' name='TeaName' onKeyUp='showHint(this.value)' value='$TeaName' onClick='cleartbox()' size='35'/>";
	  ?></td>
      <td width="274" bgcolor="#6AB5FF">&nbsp;</td>
    </tr>
    <tr>
      <th scope="row">&nbsp;</th>
      <th scope="row" bgcolor="#6AB5FF">Training Name:</th>
      <td bgcolor="#999999">
	  <div id="TID"><select name="Training_Name">
        <?php
	  if(isset($_POST['SaveChanges']))
	  {	
	  $resultnet=mysql_query("SELECT * FROM training WHERE Training_Id='$Training_Idc'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Training_Name=$rownet['Training_Name'];
}
	 echo '<option value="'.$Training_Name.'">'.$Training_Name.'</option>';
	 	
	  }
	  if(isset($_POST['Edit_This']))
	  {
	   $resultnet=mysql_query("SELECT * FROM training WHERE Training_Id='$Training_Id'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Training_Name=$rownet['Training_Name'];
}
	 echo '<option value="'.$Training_Name.'">'.$Training_Name.'</option>';
	  }
	  else
	  {
	  echo '<option value="Select One">Select One</option>';
	  }
	  
	   if($Teacher_NIC!="")
	  {
	  $exa=mysql_query("SELECT DISTINCT(Training_Id) FROM training_details WHERE Teacher_NIC='$Teacher_NIC'");
	  }
	  if($Teacher_NIC=="")
	  {
	  $exa=mysql_query("SELECT DISTINCT(Training_Id) FROM training_details");
	  }
  while($row=mysql_fetch_array($exa))
  {
	 $Tra_Id=$row['Training_Id'];
	 $resultnet=mysql_query("SELECT * FROM training WHERE Training_Id='$Tra_Id'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Tra_Name=$rownet['Training_Name'];
}
	if($Training_Name!=$Tra_Name)
{
	  echo '<option value="'.$Tra_Name.'">'.$Tra_Name.'</option>';
  }
  }
	  ?>
      </select></div></td>
      <td bgcolor="#6AB5FF">&nbsp;</td>
    </tr>
    <tr>
      <th scope="row">&nbsp;</th>
      <th colspan="2" bgcolor="#6AB5FF" scope="row"><div align="center"><br/>
        <a href="Updatetra.php" class="reset">Reset</a>|
        <input name="Edit_This" align="middle" type="submit" value="Edit this" class="buttonok"/>
      </div></th>
      <td bgcolor="#6AB5FF">&nbsp;</td>
    </tr>
     
  </table>
  <br/>
  
</form>
<form action="#" method="POST">
    <?php 
	if(isset($_POST['Edit_This']))
	   {
	   	    $Training_Name=$_POST['Training_Name'];
	   	  $resultnet=mysql_query("SELECT * FROM training WHERE Training_Name='$Training_Name'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Training_Id=$rownet['Training_Id'];
}
	  
	  $TeaName=$_POST['TeaName'];
$name=explode(" ",$TeaName);
$fn=$name[0];
$ln=$name[1];
$results= mysql_query("SELECT * FROM teacher Where First_Name='$fn' AND Last_Name='$ln'");
if($row = mysql_fetch_array($results))
{
 $Teacher_NIC=$row['T_NIC_Number'];
}
	   if($Training_Name!="Select One"&&$TeaName!="" )
	   {
	   if($_SESSION['Access_Type']=="Teacher in Charge")
	   {
	$NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  $get_teaSch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$Teacher_NIC'");
      if(mysql_num_rows($get_teaSch)>0)
{
  if($rowsesch=mysql_fetch_array($get_teaSch))
{
$schid=$rowsesch['School_Id'];
if($schid==$Own_School_Id)
{
	   $result=mysql_query("SELECT * FROM training_details WHERE Training_Id='$Training_Id' AND Teacher_NIC='$Teacher_NIC' ");
}
	   }
	   }
	   }
	   }
	    else if($_SESSION['Access_Type']=="Administrator")
{
      $result=mysql_query("SELECT * FROM training_details WHERE Training_Id='$Training_Id' AND Teacher_NIC='$Teacher_NIC'");
	  }
   if(($result))
   {  
  if(mysql_num_rows($result)>0)
  { 
  echo '<table width="354" border="0" class="tablefontv">';
 while($row=mysql_fetch_array($result))
  {    
$Trai_Id=$row['Training_Id'];
$Institute=$row['Institute'];
$Year=$row['Year'];
$Teacher_NIC=$row['Teacher_NIC'];
$res=mysql_query("SELECT * FROM  training WHERE Training_Id='$Trai_Id'");
if($rowc=mysql_fetch_array($res))
{
$Training_Name=$rowc['Training_Name'];
}
$Duration=$row['Duration'];
	
   ?>
   <?php
      echo '<tr>';
        echo '<th width="200" height="44" scope="row">';
		echo "Training Id:";
		echo '</th>';
        echo '<td width="144">';
		?>
<select name="Training_Idc" id="Training_Idc">
        <?php	  
	  if($Tra_Id!="")
	  {
	 echo '<option value="'.$Trai_Id.'">'.$Training_Name.'</option>';
	  }
	  else
	  {
	  echo '<option value="Select One">Select One</option>';
	  }
	  $com=mysql_query("SELECT DISTINCT(Training_Id) FROM training");
	  
  while($row=mysql_fetch_array($com))
  {
	 $Tra_Id=$row['Training_Id'];
	 $tra_pro=mysql_query("SELECT * FROM training WHERE Training_Id='".$Tra_Id."'");
	 if($row_tra=mysql_fetch_array($tra_pro))
	 {
	 $Tra_name=$row_tra['Training_Name'];
	 }
if($Tra_Id!=$Trai_Id)
{
	  echo '<option value="'.$Tra_Id.'">'.$Tra_name.'</option>';
}
  }
 
	  ?>
      </select>	
<?php	  
		echo '</td>';
      echo '</tr>';	  
      echo '<tr>';
        echo '<th height="44" scope="row">';
		echo "Teacher NIC:";
		echo '</th>';
        echo '<td>';		
		echo "<input type='text' name='Teacher_NICc' value='$Teacher_NIC' readonly='readonly'/>";		
		echo '</td>';
      echo '</tr>';	  
      echo '<tr>';
        echo '<th height="44" scope="row">';
		echo "Institute:";
		echo '</th>';
        echo '<td>';
		echo "<input type='text' name='Institute' value='$Institute'/>";		
		echo '</td>';
		 echo '</tr>';		 
      echo '<tr>';
        echo '<th height="44" scope="row">';
		echo "Duration:";
		echo '</th>';
       echo '<td>';	  
	   echo "<input type='text' name='Duration' value='$Duration'/>";	  
	   echo '</td>';
      echo '</tr>';		 
      echo '<tr>';
        echo '<th height="44" scope="row">';
		echo "Started year:";
		echo '</th>';
       echo '<td>';	  
	   echo "<input type='text' name='Year' value='$Year'/>";	  
	   echo '</td>';
      echo '</tr>';     
	 }
   echo '</table>';
   
   echo "</br>";
   echo '<input name="SaveChanges" align="middle" type="submit" value="Save changes" class="buttonok"/>';
 }
 }
 }
	   }
	   ?>
       </form>
 </div>
 <div id="righttablereg">
  <div class="suggestionListsch" id="List"></div>
 </div>  
</div>
</div>
</div>
<div id="sidebar">
<br/>
<div id="partners" class="boxed">
			<h2 class="title">TRAINING DETAILS</h2>
            </div> 
<?php
include_once "menu.php";
?>
<?php
include_once "../footer.php";
?>
<?php

}
?>
</body>
</html>